#include "Buffer.h"
#include "Timer.h"
#include "CrankCamController.h"
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestCase.h>

class CrankCamControllerTest: public CppUnit::TestFixture {
private:

public:
	virtual void setUp() {
	}

	virtual void tearDown() {
	}

	void testAngles() {

		Timer timer;
		bool pattern[] = {1,1,1,0};
		CrankCamController::MaskBufType toothPattern(pattern, 4);
		CrankCamController controller(timer, toothPattern);

		TimeUnit cycle[] = {3,3,6};
	    for (int j = 0; j < 3; ++j) {
	    	for (TimeUnit* t = cycle; t<cycle + sizeof(cycle)/sizeof(TimeUnit); t++) {
	    		TimeUnit currentTime = timer.getTime() + *t;
	    		timer.setTime(currentTime);
	    		controller.interruptToothEvent();
	    	}
		}
	    CPPUNIT_ASSERT_EQUAL((AngleUnit)0, controller.getCurrentAngle());
	    CPPUNIT_ASSERT_EQUAL((AngleUnit)(900), controller.predictAngleForTime(timer.getTime()+3));
	    CPPUNIT_ASSERT_EQUAL((TimeUnit)(timer.getTime()+3), controller.predictTimeForAngle(900));

	    // TODO now: need much more tests, check other tooth patterns
	}

	void testAlmostEqual () {
		CPPUNIT_ASSERT (almostEqual(1, 1));
		CPPUNIT_ASSERT (almostEqual(100, 101));
		CPPUNIT_ASSERT (almostEqual(102, 105));
		CPPUNIT_ASSERT (almostEqual(102, 120));
		CPPUNIT_ASSERT (almostEqual(120, 102));
		CPPUNIT_ASSERT (!almostEqual(160, 102));
	}

CPPUNIT_TEST_SUITE( CrankCamControllerTest );
	CPPUNIT_TEST( testAngles );
	CPPUNIT_TEST(testAlmostEqual);
CPPUNIT_TEST_SUITE_END();

};
